<?php

class RmaController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $rmaMapper = new Application_Model_RmaMapper();
        
        $rmaList = $rmaMapper->fetchAllRmaRequests();
        
        $this->view->rmaList = $rmaList;
        $this->view->pageTitle = 'List all Rma requests';
    }
    
    public function addAction()
    {
    	$form = new Application_Form_RmaAdd();
    	
    	$request = $this->getRequest();
    	
    	$this->view->pageTitle = 'Add Item for Pricelist';
    	
    	if($request->isPost()) {
    		if ($form->isValid($request->getPost())) {
    			$table = new Application_Model_DbTable_RmaRequest();
    			
    			try {
    				$id = $table->insert($form->getValues());
    			} catch(Exception $e) {
    				echo $e->getMessage();
    			}
    			$this->_redirect('rma/edititems/id/'.$id);
    		}
    	}
    	
    	$this->view->pageTitle='Create RMA request';
    	$this->view->form = $form;
    }
    
    public function edititemsAction()
    {    	
    	//get rmarequest id
    	$request = $this->getRequest();
    	$rma_id = $request->getParam('id');
    	
    	$this->view->pageTitle = 'Edit items to RMA request';
    	
    	
    	$rmaMapper = new Application_Model_RmaMapper();
    	$rmaStatus = $rmaMapper->getRmaStatusById($rma_id);
    	
    	//get RMArequest info
    	$rmaMapper = new Application_Model_DbTable_RmaRequest();
    	$rmaInfo = $rmaMapper->find($rma_id)->toArray();
    	
    	$this->view->rmaInfo = $rmaInfo;
    	
    	//check if RMA Request is already approved. if approved editing is not allowed
    	if($rmaStatus['status'] == 'entered')
    	{
    		//create form and set action to rma request id
	    	$form = new Application_Form_RmaAdditems();
	    	$form->setAction('/rma/edititems/id/'.$rma_id);
	    	$form->getElement('rma_id')->setValue($rma_id);
	    	
	    	$rmaitemsMapper = new Application_Model_DbTable_RmaRequestItems();
	    	
	    	if($request->isPost()) {
	    		if ($form->isValid($request->getPost())) {
	    			
	    			//insert rma item into db
	    			$rmaitemsMapper->insert($form->getValues());
	    			$form->reset();
	    			
	    		}
	    	}
	    	
	    	
	    	//get already inserted rma items
	    	
	    	$rmaItems = $rmaitemsMapper->fetchAll('rma_id='.$rma_id)->toArray();
	    	
	    	//submit paramaters to view controller
	    	$this->view->rmaItems = $rmaItems;
	    	$this->view->form = $form;
	    	

    	}
    	else {
    		$this->view->errorMsg = 'RmaRequest is already approved, editing is not allowed!';
    	}
    	
    	
    } 
    
    public function deleteitemsAction() {
    	
    	$rmaitemsMapper = new Application_Model_DbTable_RmaRequestItems();
    	$id = $this->getParam('id');
    	$rmaitemsMapper->delete('id='.$id);
    	$this->_helper->json('success');
    }
    
    public function sendmailAction()
    {
    	$dbmapper = new Application_Model_PendingRmaMapper();
    	
    	$this->view->amountTable = $dbmapper->fetchAmountByReasoncodeAndCustomer();
    	
    	$this->view->quantityTable = $dbmapper->fetchQtyByReasoncodeAndDivision();
    	
    	
    	 $htmlString = $this->view->render('rma/pendingrma.phtml');
    	
    	$mail = new Zend_Mail();
    	$mail->setBodyHtml($htmlString);
    	$mail->addTo('felix.kauschmann@activision.com');
    	$mail->setSubject('TestBetreff');
    	$mail->send();
    	
    	$this->_helper->json("foo");
    }

    public function pendingrmaAction()
    {
    	$this->view->pageTitle ='Pending RMA overview';
    	
        $dbmapper = new Application_Model_PendingRmaMapper();
        
        $this->view->amountTable = $dbmapper->fetchAmountByReasoncodeAndCustomer();
        
        $this->view->quantityTable = $dbmapper->fetchQtyByReasoncodeAndDivision();
        
        /*
        $htmlString = $this->view->render('rma/pendingrma.phtml');
        
        $mail = new Zend_Mail();
        $mail->setBodyHtml($htmlString);
        $mail->addTo('felix.kauschmann@activision.com');
        $mail->setSubject('TestBetreff');
        $mail->send();
        
        //$this->view->entries = $dbmapper->fetchAmountByReasoncodeAndCustomer();
        
        $query = $db->select()
        			->from(array('p' => 'tbl_pendingRma'), array('pendingBlizzard' => 'SUM(CASE WHEN i.divisionDesc=\'DIV_BLIZZARD\' THEN p.pendingQty*p.sellingPrice ELSE 0 END)',
        														'pendingCOD' => 'SUM(CASE WHEN i.divisionDesc=\'DIV_CALL_OF_DUTY\' THEN p.pendingQty*p.sellingPrice ELSE 0 END)',
        														'pendingSkylanders' => 'SUM(CASE WHEN i.divisionDesc=\'DIV_SKYLANDERS\' THEN p.pendingQty*p.sellingPrice ELSE 0 END)',
        														'pendingLegacy' => 'SUM(CASE WHEN i.divisionDesc=\'DIV_LEGACY_3100\' THEN p.pendingQty*p.sellingPrice ELSE 0 END)',
        														'pendingAmp' => 'SUM(CASE WHEN i.divisionDesc=\'DIV_AMP\' THEN p.pendingQty*p.sellingPrice ELSE 0 END)',
        														'pendingLucas' => 'SUM(CASE WHEN i.divisionDesc=\'DIV_LUCAS\' THEN p.pendingQty*p.sellingPrice ELSE 0 END)',
        														'pendingHero' => 'SUM(CASE WHEN i.divisionDesc=\'DIV_HERO\' THEN p.pendingQty*p.sellingPrice ELSE 0 END)'))
        			->joinLeft(array('i' => 'tbl_inventoryItem'),'i.inventoryItem=p.inventoryItem', 'i.divisionDesc')
        			->joinLeft(array('c' => 'tbl_customer'), 'c.customerNumber=p.customerNumber', null)
        			->joinLeft(array('cs' => 'tbl_customer_salesrep'), 'cs.customer_partyNumber = c.partyNumber', null)
        			->joinLeft(array('s' => 'tbl_salesrep'), 's.id = cs.salesrep_id', 's.salesRepName')
        			->group(array('s.salesrepName'));
        
        $budget = $query->query()->fetchAll();
        
        
        $this->view->entries = $budget;*/
        
        	
    }
    
    public function exportrmauploadAction() 
    {	
    	//get all entered rmaRequest from Database 
    	$db = Zend_Db_Table::getDefaultAdapter()->select()
    												->from(array('r' => 'bis.tbl_rmarequest'), array('rmaReason', 'sapCode'))
    												->where('status=\'approved\'')
    												->joinLeft(array('ri' => 'bis.tbl_rmarequestitems'),'ri.rma_id = r.id', array('inventoryItem', 'rmaQuantity'));
    	
    	$result = $db->query()->fetchAll();
    	
    	//create unique file in rmaupload folder
    	$filepath = 'rmaupload/'.time().'.csv';
    	
    	//archive filepath to database
    	
    	//tbd
    	
    	//create uploadfile
    	$file = fopen($filepath, 'w');
    	
    	//write data into csv file
    	foreach($result as $field) {
    		fputcsv($file, $field);
    	}
    	
    	//close file
    	fclose($file);
    	
    	//submit filepatch to view in order to create download link
    	$this->view->filePath = $filepath;
    }

}



